package com.controllor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.OrdinaryEmployeesDao;
import com.entity.User;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.util.JSON_to_layui;

/**
 * Servlet implementation class OrdinaryEmployeesServlet
 */
@WebServlet("/OrdinaryEmployeesServlet")
public class OrdinaryEmployeesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public OrdinaryEmployeesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@SuppressWarnings("null")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		User USER = (User) session.getAttribute("user");
		String userid = USER.getUserid();
//		String userid = (String)session.getAttribute("userid");
//*****“lqq”临时测试，记得改为“userid”*********
//		String userid = "lqq";
		String button = request.getParameter("button");
//		String button ="1";
		
		//获取单个用户基本信息 或 薪资信息
		if("1".equals(button)) {
			System.out.println("Ordianry..button"+button);
			ObjectMapper mapper = new ObjectMapper();
			mapper.writeValue(response.getWriter(), OrdinaryEmployeesDao.getUserInfo(userid,null,null,false));
		}
		//异步修改信息
		else if("2".equals(button)) {
			System.out.println("Ordianry..button"+button);
			PrintWriter out = response.getWriter();
			String field = request.getParameter("field");
			String newinfo = request.getParameter("newinfo");
			OrdinaryEmployeesDao.updateUserInfo(userid, field,newinfo);
		}
		//修改密码
		else if("3".equals(button)) {
			System.out.println("Ordianry..button"+button);
			PrintWriter out = response.getWriter();
			String password = request.getParameter("password");
			if(OrdinaryEmployeesDao.updateUserInfo(userid, "pwd",password)) {
				out.print("<script>alert('修改成功');window.location.href='ordinary/02updatePwd.jsp';</script>");
			}else {
				out.print("<script>alert('修改失败');window.location.href='ordinary/02updatePwd.jsp';</script>");
			}
		}
		//加班录入
		else if("4".equals(button)) {
			System.out.println("Ordianry..button"+button);
			PrintWriter out = response.getWriter();
			int overtime = Integer.parseInt(request.getParameter("overtime"));
			int weekovertime = Integer.parseInt(request.getParameter("weekovertime"));
			int festovertime = Integer.parseInt(request.getParameter("festovertime"));
			String applydescribe = request.getParameter("applydescribe");
			if(OrdinaryEmployeesDao.inputOvertime(userid, overtime, weekovertime, festovertime, applydescribe)) {
				out.print("<script>alert('已提交待审核');window.location.href='ordinary/03inputOvertime.jsp';</script>");
			}else {
				out.print("<script>alert('提交失败，可能需要重新录入');window.location.href='ordinary/03inputOvertime.jsp';</script>");
			}
		}
		//查阅审核表
		else if("5".equals(button)) {
			System.out.println("Ordianry..button"+button);
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = (Integer.parseInt(request.getParameter("page"))-1) * limit;
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(OrdinaryEmployeesDao.getSelfApply(userid,page,limit));
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,OrdinaryEmployeesDao.count,"");
			}
			response.getWriter().print(json);
//			mapper.writeValue(response.getWriter(), OrdinaryEmployeesDao.getSelfApply(userid));
		}
		//获取薪资信息
		else if("6".equals(button)) {
			System.out.println("Ordianry..button"+button);
			ObjectMapper mapper = new ObjectMapper();
			mapper.enable(SerializationFeature.INDENT_OUTPUT);	//序列化
			//给网页返回数据
			String jsonString = mapper.writeValueAsString(OrdinaryEmployeesDao.getUserInfo(userid,null,null,false));
			response.getWriter().print(jsonString);
			
			//读取json文件
//			JsonNode jsonNode = mapper.readTree(new File(session.getServletContext().getRealPath("")+"\\public\\api\\temp.json"));
//			System.out.println(jsonNode);
//			String json = mapper.writeValueAsString(jsonNode);
//			response.getWriter().print(json);
			/*
			try {
				//构建json树
				JsonNode data = mapper.readTree(jsonString);//字符串转json对象，数据源
				
				JsonNode jsonNode = mapper.readTree("{}");
				ObjectNode newNode = mapper.createObjectNode();	//在此处构建新的json对象
				ArrayNode arrayNode = mapper.createArrayNode();
				System.out.println("jsonNode="+jsonNode);	//测试data值
				System.out.println("newNode="+newNode);
				
				//因为jsonNode不能直接put（）
			    ObjectNode objectNode = (ObjectNode) jsonNode;
			    objectNode.put("first","1");
			    System.out.println("mid jsonNode="+jsonNode);
			    System.out.println("objectNode="+objectNode);	//发现二者是同步的！
			    
			    arrayNode.add(objectNode);
			    System.out.println("arrayNode1="+arrayNode);
			    objectNode.put("second", "2");
//			    arrayNode.add(objectNode);
			    System.out.println("arrayNode2="+arrayNode);
			    
				//通过at以路径方式获取获取键值
//				System.out.println(data.at("/userid").asText());
				newNode.put("name", "lqqname");
				newNode.put("children", "lqq");
				newNode.put("temp", "");
				jsonNode = newNode.set("Child", jsonNode);
				jsonNode = newNode.set("Child2", arrayNode);
				System.out.println("After jsonNode="+jsonNode);
				System.out.println("After newNode="+newNode);
				System.out.println("路径："+session.getServletContext().getRealPath("")+"public\\api\\temp2.json");
				OutputStream outputStream = new FileOutputStream(new File("D:\\Soft_Workspace\\eclipse-workspace\\Staff system\\WebContent\\public\\api\\temp2.json"));
				mapper.writeValue(outputStream, jsonNode);
				
//				File file = new File("temp2.json");
//				String path = file.getAbsolutePath();
//				System.out.println("path="+path);
//				
//				String path2 = this.getServletContext().getRealPath("temp2.json");
//				System.out.println("path2="+path2);
				
				
			}catch( IOException e) {
				e.printStackTrace();
			}
			*/
		}
		//获取全部职工的 基本信息 或薪资信息
		else if("7".equals(button)) {
			System.out.println("Ordianry..button"+button);
//			String conditionValue = request.getParameter("conditionValue");
//			String condition = request.getParameter("condition");
//			
//			System.out.println("conditionValue"+conditionValue);
//			ObjectMapper mapper = new ObjectMapper();
//			String json = mapper.writeValueAsString(OrdinaryEmployeesDao.getUserInfo(userid,condition,conditionValue,true));
//			if(json != null || json.length() != 0) {
//				json = JSON_to_layui.getLayuiData(json,0,OrdinaryEmployeesDao.count,"");
//			}
//			response.getWriter().print(json);
		}
		else if("8".equals(button)) {
			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		
	}

}
